// * Element 常用表单校验规则
/**
 * @name rule--验证规则
 * @name value--需要验证的内容
 * @name callback--回调函数，验证通过需要放行该函数
 */


/**
 *  @rule 手机号
 */
export function checkPhoneNumber(rule, value, callback) {
	const regexp = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/;
	if (value === "") callback("请输入手机号码");
	if (!regexp.test(value)) {
		callback(new Error("请输入正确的手机号码"));
	} else {
		return callback();
	}
}


/***
* @description: 校验身份证
* @param {*}
* @return {*}
* @author: white_queen
*/
export function checkIdCard (idCardNumber) {
  if (!idCardNumber || typeof idCardNumber !== 'string' || idCardNumber.length !== 18) return false

  const coefficient = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
  const checkCode = [1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2]
  const code = idCardNumber.substring(17)

  let sum = 0
  for (let i = 0; i < 17; i++) {
    sum += Number(idCardNumber.charAt(i)) * coefficient[i]
  }

  return checkCode[sum % 11].toString() === code.toLowerCase()
}


/***
* @description: 校验密码 字母+数字 6-16位
* @param {*}
* @return {*}
* @author: white_queen
*/
export function checkPssword (value) {
	const weak = /^(?=.*[0-9])(?=.*[a-zA-Z])[0-9a-zA-Z~!@#$%^&*_\-+=`|\\(){}[\]:;"'<>,.?/]{6,16}$/
	if (!weak.test(value)) {
		return false
	} else {
		return true;
	}
}

/***
* @description: 校验邀请码
* @param {*}
* @return {*}
* @author: white_queen
*/
export function checkInvite (value) {
	const weak = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6}$/
	if (!weak.test(value)) {
		return false
	} else {
		return true;
	}
}